package com.tjtt.gwm.dao.impl;

import com.tjtt.tdm.base.BaseHibernateDao;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.lang.*;
import java.util.*;
import org.hibernate.Query;
import com.google.common.base.Objects;
import com.jcraft.jsch.Session;
import com.tjtt.gwm.bean.*;
import com.tjtt.gwm.dao.*;
import com.tjtt.tdm.base.PageResult;
import com.tjtt.tdm.base.StringUtil;
import com.tjtt.tdm.system.bean.Organization;
import com.tjtt.tdm.system.bean.Users;

public class EEPRelationsDaoImpl extends BaseHibernateDao implements EEPRelationsDao{
	
	//查询（通过备件查询设备）
	public PageResult queryReDataByEP(Map filterMap) {
		StringBuffer countString = new StringBuffer();
		countString.append("select count(*) from  TDM_EQU_EP_RELATIONS r,TDM_EQUIPMENT_PARTS ep,tdm_equipment t where r.R_EQU_ID=t.id ");
		StringBuffer queryString = new StringBuffer();	
		queryString.append("select r.R_ID,t.EQU_NAME as eName,t.EQU_NO as eNo from TDM_EQU_EP_RELATIONS r,TDM_EQUIPMENT_PARTS ep,tdm_equipment t where r.R_EQU_ID=t.id ");
		
		if(null!=filterMap && !filterMap.isEmpty()){
			if(null!=filterMap.get("epId") && !"".equals(filterMap.get("epId"))){
				queryString.append(" and r.R_EP_ID=ep.ID and ep.ID="+filterMap.get("epId"));
				countString.append(" and r.R_EP_ID=ep.ID and ep.ID="+filterMap.get("epId"));
			}
		}
		queryString.append(" order by r.R_ID desc");
	//	queryString.append(" and ep.EP_ISKY='0' order by r.R_ID desc");
	//	countString.append(" and ep.EP_ISKY='0'");

		return searchBySQL(countString.toString(),queryString.toString(),filterMap);
	}
	//根据备件id查询关系ids
	public String queryRIdsByEP(String epId){
		String ids="";
		String queryString="select r.R_ID from TDM_EQU_EP_RELATIONS r where r.R_EP_ID="+epId;
		List l=new ArrayList();
		Query query = getSession().createSQLQuery(queryString);
		l=query.list();
		if(l.size()==0){
			ids="";
		}
		else{
		  for(int i=0;i<l.size();i++) {
		     ids+=l.get(i).toString()+",";
		  }
		}
		return ids;
	}
	//根据备件id查询关系表某条数据
	public EEPRelations queryRById(String rId){
		int ID=Integer.parseInt(rId);
		return this.getHibernateTemplate().get(EEPRelations.class, ID);
	}
	//新增
	public  void addEEPRelations(EEPRelations relations){
		this.getHibernateTemplate().save(relations);
	}
	
    //删除
	public  void deleteEEPRelations(EEPRelations relations){
		this.getHibernateTemplate().delete(relations);
	}
	
	//修改
	public void updateEEPRelations(EEPRelations relations){
		this.getHibernateTemplate().update(relations);
	}
	//通过设备id查询设备信息
	public PageResult queryChooseEQUByEquIds(Map filterMap){
		StringBuffer countString = new StringBuffer();
		countString.append("select count(*) from tdm_equipment t  where 1 = 1 and ");
		StringBuffer queryString = new StringBuffer();	
		queryString.append("select t.ID as eId,t.EQU_NAME as eName,t.EQU_NO as eNo from tdm_equipment t where 1=1 and ");
		
		if(null!=filterMap && !filterMap.isEmpty()){
			if(null!=filterMap.get("equIds") && !"".equals(filterMap.get("equIds"))){
				String equIds=filterMap.get("equIds")+"";
				String ids[]=equIds.split(",");
				for(int i=0;i<ids.length ;i++){
					queryString.append("  t.ID ="+ids[i]+" or " );
					countString.append("  t.ID ="+ids[i]+" or " );
				}
				queryString.append(" 1=2 ");
				countString.append(" 1=2 " );
			}	
			else{
				queryString.append(" 2=2 ");
				countString.append(" 2=2 " );
			
			}
		}
		
		queryString.append(" order by t.ID desc");

		return searchBySQL(countString.toString(),queryString.toString(),filterMap);
	}
	//通过设备id查询设备信息（全）
	public TdmEquipment queryEqu(String id){
		Long ID=Long.parseLong(id);
		return this.getHibernateTemplate().get(TdmEquipment.class, ID);
	}
	//通过备件id查询备件信息（全）
	public EquipmentParts queryEPInfo(String id){
		BigDecimal ID=new BigDecimal(id);
		return this.getHibernateTemplate().get(EquipmentParts.class, ID);
	}
	
	

}
